/************************************************************************/
/* /* 转载自网络.                                                                          */
/************************************************************************/
/***********************************************

具有1--5个参数的模板的局部特化

************************************************/



//函数调用类抽象接口特化
template<TYPES_DECLARE>
class FunctionInterface<ALL_TYPES>
{
public:
	virtual void* GetClassHandler()=0;
	virtual void* GetFunHandler()=0;
	virtual void Invoke(USEFUL_TYPES)=0;
};


//全局函数调用类特化
template<TYPES_DECLARE>
class GolobalFunction<ALL_TYPES>:public FunctionInterface<USEFUL_TYPES>
{
public:
	typedef void(*T_HANDLER)(USEFUL_TYPES);
protected:
	union
	{
		T_HANDLER FuncHandler;
		void*   pFPointer;
	};

public:   
	GolobalFunction(T_HANDLER f)
	{
		FuncHandler=f;
	}
	void *GetClassHandler()
	{
		return 0;
	}
	void *GetFunHandler()
	{
		return pFPointer;
	}
	void Invoke(PARAMS_DECLARE)
	{
		(*FuncHandler)(PARAMS);
	}
};

//成员函数调用类特化
template<typename Class,TYPES_DECLARE>
class MemberFunction<typename Class,ALL_TYPES>:public FunctionInterface<USEFUL_TYPES>
{
public:
	typedef void(Class::*T_HANDLER)(USEFUL_TYPES);
protected:
	union
	{
		T_HANDLER FuncHandler;
		void *  pFPointer;
	};
	union
	{
		Class *OwnClassHandler;
		void * pCPointer;
	};

public:
	MemberFunction(Class *pClass,T_HANDLER f)
	{
		OwnClassHandler=pClass;
		FuncHandler=f;
	}
	void *GetClassHandler()
	{
		return pCPointer;
	}
	void *GetFunHandler()
	{
		return pFPointer;
	}
	void Invoke(PARAMS_DECLARE)
	{
		(OwnClassHandler->*FuncHandler)(PARAMS);
	}
};


//事件类特化
template<TYPES_DECLARE>
class Event<ALL_TYPES>
{
protected:
	typedef std::list< FunctionInterface<USEFUL_TYPES>*> T_FUNCTIONLIST;
	typedef typename T_FUNCTIONLIST::iterator T_FUNCTION_ITERATOR;

	T_FUNCTIONLIST FunctionList;

private:
	T_FUNCTION_ITERATOR Find(FunctionInterface<USEFUL_TYPES>* target)
	{
		void *temp1=target->GetClassHandler();
		void *temp2=target->GetFunHandler();
		T_FUNCTION_ITERATOR i=FunctionList.begin();
		for (;i!=FunctionList.end();++i)
		{
			if (temp1==(*i)->GetClassHandler()  &&  temp2==(*i)->GetFunHandler())
			{
				break;
			}
		}
		return i;
	}
public:
	inline void Invoke(PARAMS_DECLARE)
	{
		T_FUNCTION_ITERATOR i=FunctionList.begin();
		for (;i!=FunctionList.end();++i)
		{
			(*i)->Invoke(PARAMS);
		}
	}

	void Bind(typename GolobalFunction<USEFUL_TYPES>::T_HANDLER gFuncHandle)
	{
		GolobalFunction<USEFUL_TYPES>*addone=new GolobalFunction<USEFUL_TYPES>(gFuncHandle);
		if(Find(addone)!=FunctionList.end())//已经存在了,就不需要添加
			delete addone;//删除临时的new对象
		else
			FunctionList.push_back(addone);
	}
	void UnBind(typename GolobalFunction<USEFUL_TYPES>::T_HANDLER gFuncHandle)
	{
		GolobalFunction<USEFUL_TYPES>*delone=new GolobalFunction<USEFUL_TYPES>(gFuncHandle);
		T_FUNCTION_ITERATOR delnode=Find(delone);
		if (delnode!=FunctionList.end())//存在，删除节点和指针
		{
			delete (*delnode);
			FunctionList.erase(delnode);
		}
		delete delone;//删除临时的new对象
	}
	template<typename Class>
	void Bind(Class *classHandle,typename MemberFunction<Class,USEFUL_TYPES>::T_HANDLER mFuncHandle)
	{
		MemberFunction<Class,USEFUL_TYPES>*addone=new MemberFunction<Class,USEFUL_TYPES>(classHandle,mFuncHandle);
		if (Find(addone)!=FunctionList.end())//已经存在
			delete addone;//删除临时的new对象
		else
			FunctionList.push_back(addone);
	}
	template<typename Class>
	void UnBind(Class *classHandle,typename MemberFunction<Class,USEFUL_TYPES>::T_HANDLER mFuncHandle)
	{
		MemberFunction<Class,USEFUL_TYPES>*delone=new MemberFunction<Class,USEFUL_TYPES>(classHandle,mFuncHandle);
		T_FUNCTION_ITERATOR delnode=Find(delone);
		if (delnode!=FunctionList.end())//存在，删除节点和指针
		{
			delete (*delnode);
			FunctionList.erase(delnode);
		}
		delete delone;//删除临时的new对象
	}
	void operator()(PARAMS_DECLARE)
	{
		Invoke(PARAMS);
	}
	~Event()
	{
		T_FUNCTION_ITERATOR i=FunctionList.begin();
		for (;i!=FunctionList.end();++i)
		{
			delete (*i);
		}
	}
};